fix: preserve $ characters in interpolated error message values#34220
Open
anxkhn wants to merge 2 commits into
Open
fix: preserve $ characters in interpolated error message values#34220anxkhn wants to merge 2 commits into
anxkhn wants to merge 2 commits into
Conversation
Collaborator
|
Member
|
@anxkhn Please add a changelog entry |
stripIndent() substitutes each interpolated value into its errTemplate placeholder with String.prototype.replace and a string replacement. Because the search argument is a literal placeholder (no capture groups), a value containing `$&`, `$\``, `$'` or `$$` was interpreted as a special replacement pattern and corrupted the rendered message. Such values are common, for example JSON-stringified data embedded in an error. Pass a replacer function so the value is inserted literally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Additional details
stripIndent()(packages/errors/src/stripIndent.ts) is the substitution engine behind theerrTemplatetagged-template system that formats Cypress error messages.errTemplatecallsstripIndent(templateStrings, ...templateArgs)andstripIndentsplices each interpolated value into a<<i>>placeholder with:String.prototype.replacewith a string replacement interprets$sequences in that replacement. Since the search argument here is a literal placeholder (no capture groups), the sequences that apply are$&(the matched placeholder),$`(text before the match),$'(text after the match), and$$(an escaped$). So any interpolated value containing one of those was corrupted in the rendered error. For example, an error that embeds JSON-stringified data:{"expr":"a$&b"}... {"expr":"a<<0>>b"}(the raw<<0>>placeholder leaks back in via$&)... {"expr":"a$&b"}The fix passes a replacer function instead of a string. A function's return value is inserted literally and is not subject to
$-pattern interpretation, so values are rendered verbatim. This is a one-line change with no behavior change for any value that does not contain those$sequences; the package's 160 error-render snapshot tests are unaffected.This is a self-identified correctness fix with no linked issue. Per
CONTRIBUTING.md, purely mechanical/correctness fixes may be submitted without a prior issue as long as the reasoning is explained here. Happy to open a tracking issue first if the maintainers prefer.Note
Low Risk
One-line behavioral fix in internal error templating with a targeted unit test; no public API or runtime test-path changes beyond clearer error messages.
Overview
Fixes garbled Cypress error text when
errTemplate/stripIndentsplice interpolated values that contain JavaScript replacement patterns ($&,$`,$',$$).stripIndentnow passes a replacer function toString.prototype.replaceinstead of a string replacement, so interpolated args (for example JSON in config validation errors) render verbatim. A unit test covers the regression, and 15.18.1 changelog documents the user-facing fix.Reviewed by Cursor Bugbot for commit 25e3109. Bugbot is set up for automated code reviews on this repo. Configure here.
Steps to test
A new unit test in
packages/errors/test/stripIndent.spec.tscovers this. To reproduce manually:yarn workspace @packages/errors test -- test/stripIndent.spec.ts$&,$`,$'and$$and asserts it renders verbatim. On the previous code it renders the corrupted string (e.g. leaking<<0>>and re-inserting surrounding text); with this change it renders the value unchanged.The full package suite (
yarn workspace @packages/errors test, 184 tests including the 160visualSnapshotErrorsrenders) andyarn workspace @packages/errors check-tsboth pass.How has the user experience changed?
Cypress error messages that interpolate a value containing
$&,$`,$'or$$(for example JSON-stringified data shown in a config-validation error) are now rendered exactly as-is instead of being garbled by JavaScript's replacement-pattern handling. There is no visual change for messages whose interpolated values do not contain those sequences.PR Tasks
cypress-documentation?type definitions?